<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

require_once ROOT_PATH.'lib/baidu_bcs/bcs.func.php';

function make_thumb_image($image_name, $image_file, $image_dir)
{
	require_once ROOT_PATH.'lib/io.han.php';
	$IoHandler = new IoHandler();
		
	$image_path = ROOT_PATH.$image_dir;

	if(!is_dir($image_path))
	{
		$IoHandler->MakeDir($image_path);
	}

	$thumbwidth = $thumbheight = 0;
	$image_small = $image_path.$image_name . "_s.jpg";

	$size = IMAGE_SMALL;
	
	list($thumbwidth, $thumbheight) = explode('*', $size);
	$result = image_resize($image_file, $image_small, $thumbwidth, $thumbheight);

	if(!is_file($image_small))
	{
		@copy($image_file, $image_small);
	}
	
	return true;
}

function move_image_to_path($field, $path)
{
	require_once ROOT_PATH.'lib/io.han.php';
	$IoHandler = new IoHandler();

	$type = trim(strtolower(end(explode(".", $_FILES[$field]['name']))));
	if($type != 'gif' && $type != 'jpg' && $type != 'jpeg' && $type != 'png')
	{
		return false;
	}

	$name = substr(md5($_FILES[$field]['name']),-10);
	$image_name = $name.'_o'.".{$type}";
	$abs_path = ROOT_PATH.$path;

	if(!is_dir($abs_path))
	{
		$IoHandler->MakeDir($abs_path);
	}

	require_once ROOT_PATH.'lib/upload.han.php';
	$UploadHandler = new UploadHandler($_FILES, $abs_path, $field, true);
	$UploadHandler->setMaxSize(2048);
	$UploadHandler->setNewName($image_name);
	$result = $UploadHandler->doUpload();

	if($result && is_image($abs_path.$image_name))
	{
		make_thumb_image($name, $abs_path.$image_name, $path);
		
		return $path . $image_name;
	}
	else
	{
		//$IoHandler->RemoveDir($cache_image_path);
		return false;
	}
}

function delete_image($file)
{
	require_once ROOT_PATH.'lib/io.han.php';
	$IoHandler = new IoHandler();
	
	if(is_file($file))
	{
		$IoHandler->DeleteFile($file);
		
		if(stripos($file, '_o.') !== false)
		{
			$small = str_replace('_o.', '_s.', $file);
			if(is_file($small)) $IoHandler->DeleteFile($small);
		}
	}
}

/**
 *
 * 将字节流转成图片文件，并构造 $_FILES 数组
 *
 * @param $img_str string: Image string buffer
 * @param $img_type string: Image type
 *
 * @return $files array
 */
function save_image_file($img_str, $img_type='jpeg'){
	$files = array();

	$img_type = getImageType($img_type);

	if($img_str && $img_type)
	{
		$img_str = base64_decode($img_str);
		
		//var_dump("--- base64_decode --- ".$img_str);

		$im = imagecreatefromstring($img_str);
		
		//var_dump('--- imagecreatefromstring --- '.$im);
		
		$valid = ($im !== false);
		imagedestroy($im);

		// accept image size < 2M
		if($valid && strlen($img_str) < 2*1024*1024)
		{
			$fileName = tempnam(ROOT_PATH.'data/tmp', 'oye');
			//var_dump($fileName);

			file_put_contents($fileName, $img_str);
			unset($img_str);

			chmod($fileName, 0777);

			$files['name'] = basename($fileName).'.'.$img_type;
			$files['type'] = 'image/'.$img_type;
			$files['tmp_name'] = $fileName;
			$files['error'] = 0;
			$files['size'] = filesize($fileName);
		}
	}

	return $files;
}

/**
 *
 * 检查图片类型
 *
 * @param $type string
 *
 * @return $type string: Supported image type
 */
function getImageType($type){
	$typeName = '';
	$type = strtolower($type);

	if($type)
	{
		switch($type)
		{
			case 'gif':
				$typeName = 'gif';
				break;
			case 'jpeg':
				$typeName = 'jpeg';
				break;
			case 'jpg':
				$typeName = 'jpeg';
				break;
			case 'png':
				$typeName = 'png';
				break;
			default:
				break;
		}
	}

	return $typeName;
}

function save_image($img_name, $img_str)
{
	$url = '';

	if($img_str)
	{
		$img_str = base64_decode($img_str);

		//var_dump("--- base64_decode --- ".$img_str);

		$im = imagecreatefromstring($img_str);

		//var_dump('--- imagecreatefromstring --- '.$im);

		$valid = ($im !== false);
		imagedestroy($im);

		//var_dump('--- image valid ---', $valid, ', length='.strlen($img_str));
		
		// accept image size < 2M
		if($valid && strlen($img_str) < 2*1024*1024)
		{
			$url = save_to_bcs($img_name, $img_str);
		}
	}
	
	return $url;
}